package com.ruoyi.financial.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.financial.domain.FThingType;
import com.ruoyi.financial.service.IFThingTypeService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.domain.Ztree;

/**
 * 收支项目Controller
 * 
 * @author xunzhe
 * @date 2024-08-07
 */
@Controller
@RequestMapping("/financial/thingType")
public class FThingTypeController extends BaseController
{
    private String prefix = "financial/thingType";

    @Autowired
    private IFThingTypeService fThingTypeService;

    @RequiresPermissions("financial:thingType:view")
    @GetMapping()
    public String thingType()
    {
        return prefix + "/thingType";
    }

    /**
     * 查询收支项目树列表
     */
    @RequiresPermissions("financial:thingType:list")
    @PostMapping("/list")
    @ResponseBody
    public List<FThingType> list(FThingType fThingType)
    {
        List<FThingType> list = fThingTypeService.selectFThingTypeList(fThingType);
        return list;
    }

    /**
     * 导出收支项目列表
     */
    @RequiresPermissions("financial:thingType:export")
    @Log(title = "收支项目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FThingType fThingType)
    {
        List<FThingType> list = fThingTypeService.selectFThingTypeList(fThingType);
        ExcelUtil<FThingType> util = new ExcelUtil<FThingType>(FThingType.class);
        return util.exportExcel(list, "收支项目数据");
    }

    /**
     * 新增收支项目
     */
    @GetMapping(value = { "/add/{typeId}", "/add/" })
    public String add(@PathVariable(value = "typeId", required = false) Long typeId, ModelMap mmap)
    {
        if (StringUtils.isNotNull(typeId))
        {
            mmap.put("fThingType", fThingTypeService.selectFThingTypeByTypeId(typeId));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存收支项目
     */
    @RequiresPermissions("financial:thingType:add")
    @Log(title = "收支项目", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(FThingType fThingType)
    {
        if(fThingType.getParentId()==null){
            fThingType.setParentId(0L);
        }

        List<FThingType> list = fThingTypeService.checkName(fThingType.getTypeName());
        for(FThingType data : list){
            if(data.getParentId().longValue() == fThingType.getParentId().longValue()){
                return AjaxResult.warn("物品名称重复");
            }
        }

        return toAjax(fThingTypeService.insertFThingType(fThingType));
    }

    /**
     * 修改收支项目
     */
    @RequiresPermissions("financial:thingType:edit")
    @GetMapping("/edit/{typeId}")
    public String edit(@PathVariable("typeId") Long typeId, ModelMap mmap)
    {
        FThingType fThingType = fThingTypeService.selectFThingTypeByTypeId(typeId);
        mmap.put("fThingType", fThingType);
        return prefix + "/edit";
    }

    /**
     * 修改保存收支项目
     */
    @RequiresPermissions("financial:thingType:edit")
    @Log(title = "收支项目", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(FThingType fThingType)
    {
        List<FThingType> list = fThingTypeService.checkName(fThingType.getTypeName());
        for(FThingType data : list){
            if(data.getTypeId().longValue() != fThingType.getTypeId().longValue()){
                if(data.getParentId().longValue() == fThingType.getParentId().longValue()){
                    return AjaxResult.warn("名称重复");
                }
            }
        }

        return toAjax(fThingTypeService.updateFThingType(fThingType));
    }

    /**
     * 删除
     */
    @RequiresPermissions("financial:thingType:remove")
    @Log(title = "收支项目", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{typeId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("typeId") Long typeId)
    {
        int i = fThingTypeService.checkCount(typeId);
        if(i > 0){
            return AjaxResult.warn("存在下级节点,不允许删除");
        }

        return toAjax(fThingTypeService.deleteFThingTypeByTypeId(typeId));
    }

    /**
     * 选择收支项目树
     */
    @GetMapping(value = { "/selectThingTypeTree/{typeId}", "/selectThingTypeTree/", "/selectThingTypeTree/{typeId}/{type}", })
    public String selectThingTypeTree(@PathVariable(value = "typeId", required = false) Long typeId, ModelMap mmap,@PathVariable(value = "type", required = false) Long type)
    {
        if (StringUtils.isNotNull(typeId))
        {
            mmap.put("fThingType", fThingTypeService.selectFThingTypeByTypeId(typeId));
        }
        mmap.put("type", type);
        return prefix + "/tree";
    }

    /**
     * 加载收支项目树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = fThingTypeService.selectFThingTypeTree();
        return ztrees;
    }

    @GetMapping("/checkCount/{typeId}")
    @ResponseBody
    public AjaxResult checkCount(@PathVariable("typeId") Long typeId) {
        int i = fThingTypeService.checkCount(typeId);
        return AjaxResult.success("成功",i);
    }


}
